package org.ihelpuoo.orm.handler.rs;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.List;

import org.ihelpuoo.db.ResultSetProxy;
import org.ihelpuoo.lang.ObjectUtils;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.filter.Filter;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.ihelpuoo.orm.handler.FieldHandler;
import org.ihelpuoo.util.Pager;

public class RsReadObjectFieldHandler extends FieldHandler
{

	public RsReadObjectFieldHandler(ObjectProcessor beanHandler)
	{
		super(beanHandler);
	}

	@Override
	protected void processWithoutExceptionHandle(ObjectRule br, Field f, Object src, Object obj, FieldFilter actived, FieldFilter locked, String prefix)
			throws Exception
	{
		Class subType = f.getType();
		Filter filter = ResultSetSequenceFieldUtils.evalObjectFilter(br,f,obj);
		Connection conn = ((ResultSetProxy)src).getConnection();
		ObjectRule subRule = processor.getManual().getRule(subType);
		Pager pager = new Pager();
		pager.setPageSize(1);
		List list = processor.query(subRule,conn,subType,filter,null,null,pager,obj);
		if (null != list && list.size() > 0)
		{
			Object b = list.get(0);
			Method setter = ObjectUtils.getSetter(obj.getClass(),f);
			setter.invoke(obj,new Object[]{b});
		}
	}

}
